package killerqueen.game.objects;

import java.util.ArrayList;

import java.util.List;

import killerqueen.game.interfaces.GameObject;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Polygon;

public class CompoundObject implements GameObject {

  List<PolygonShape> polygonShapes;
  List<CircleShape> circleShapes;
  private Body body;

  public CompoundObject() {

  }

  @Override
  public void init(World world, String id) {
    polygonShapes = new ArrayList<PolygonShape>();
    circleShapes = new ArrayList<CircleShape>();

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(20, 0);
    bodyDef.type = BodyType.DYNAMIC;
    body = world.createBody(bodyDef);

    polygonShapes.add(new PolygonShape());
    polygonShapes.get(0).setAsBox(3, 3, new Vec2(-3, -3), (float) Math.PI / 4);

    //    box.set(new Vec2[] {new Vec2(-6, -6), new Vec2(0, -6), new Vec2(0, 0), new Vec2(-6, 0)}, 4);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = polygonShapes.get(0);
    fixtureDef.density = 3;
    fixtureDef.friction = 0.001f;
    fixtureDef.restitution = 0.1f;
    body.createFixture(fixtureDef);

    polygonShapes.add(new PolygonShape());
    polygonShapes.get(1).setAsBox(1, 1);
    fixtureDef.shape = polygonShapes.get(1);
    body.createFixture(fixtureDef);

    circleShapes.add(new CircleShape());
    circleShapes.get(0).m_p.set(new Vec2(3, 3));
    circleShapes.get(0).m_radius = 1f;

    FixtureDef fixtureDef2 = new FixtureDef();
    fixtureDef2.shape = circleShapes.get(0);
    fixtureDef2.density = 5;
    fixtureDef2.friction = 0.01f;
    fixtureDef2.restitution = 0.1f;
    body.createFixture(fixtureDef2);
  }

  @Override
  public void input(Input input) {
    if (input.isKeyDown(Input.KEY_Z)) {
      move(1);
    }
    if (input.isKeyDown(Input.KEY_X)) {
      move(-1);
    }
  }

  @Override
  public void paint(Graphics g) {

    g.pushTransform();

    Vec2 pos = body.getPosition();
    float angle = body.getAngle();
    g.translate(pos.x, pos.y);

    //Vec2 cent = body.getLocalCenter();
    //g.setColor(Color.red);
    //g.fillOval(-0.5f,-0.5f, 1, 1);
    g.rotate(0, 0, (float) Math.toDegrees(+angle));
    //g.setColor(Color.green);
    //g.fillOval(cent.x-0.5f, cent.y-0.5f, 1, 1);

    for (PolygonShape polygonShape : polygonShapes) {
      drawPolygon(g, polygonShape);
    }
    for (CircleShape circleShape : circleShapes) {
      drawCircle(g, circleShape);
    }
    g.popTransform();

  }

  private void drawPolygon(Graphics g, PolygonShape polygonShape) {
    Polygon p = new Polygon();
    for (int i = 0; i < polygonShape.getVertexCount(); i++) {
      Vec2 vertex = polygonShape.getVertex(i);
      p.addPoint(vertex.x, vertex.y);
    }
    g.setColor(Color.blue);
    g.fill(p);
    g.setColor(Color.black);
    g.draw(p);

  }

  private void drawCircle(Graphics g, CircleShape circleShape) {

    org.newdawn.slick.geom.Circle circle = new org.newdawn.slick.geom.Circle(circleShape.m_p.x, circleShape.m_p.y,
        circleShape.m_radius);

    g.setColor(Color.blue);
    g.fill(circle);
    g.setColor(Color.black);
    g.draw(circle);
  }

  @Override
  public void update(long delta) {
    return;

  }

  public void move(float magnitude) {
    body.applyAngularImpulse(magnitude);
  }

@Override
public void destroy(World world) {
	// TODO Auto-generated method stub
	
}

}
